package com.sec.android.app.myfiles.widget.tablet.bottomdetailview;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.sec.android.app.myfiles.feature.MouseKeyboardMgr;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.fragment.filelist.OnItemChangedListener;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationManager;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by jaebae on 2016-10-05.
 */
public class BottomDetailLayout extends RelativeLayout implements OnItemChangedListener {
    private final Context mContext;
    private BottomDetailView mBottomDetailView;
    private AbsMyFilesFragment mFragment;
    private int mProcessId;
    private ArrayList<FileRecord> mRecordList;
    private NavigationInfo mCurInfo;
    private HashMap<BottomDetailView.BottomViewType, BottomDetailView> mBottomViewMap;

    public BottomDetailLayout(Context context) {
        super(context);
        mContext = context;
    }

    public BottomDetailLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public BottomDetailLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    public void init(int processId) {
        mProcessId = processId;
        mRecordList = new ArrayList<>();
        mBottomViewMap = new HashMap<>();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        MouseKeyboardMgr.addOnItemChangedListener(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();

        MouseKeyboardMgr.removeOnItemChangedListener(this);
    }

    @Override
    public void onItemLoadFinished() {
        Log.d(this, "onItemLoadFinished");
        if (isCurInfoValid()) {
            if (MouseKeyboardMgr.isCtrlCopied() || MouseKeyboardMgr.isCtrlMoved()) {
                updateForBottomDetailBTView();
            } else {
                if (mFragment.isSelectActionMode()) {
                    updateForBottomDetailView();
                } else {
                    updateForBottomDetailLeftFocusedView();
                }
            }
        }
    }

    @Override
    public void onCheckedItemChanged() {
        Log.d(this, "OnCheckedItemChanged");
        if (isCurInfoValid()) {
            if (mFragment.isSelectActionMode() || mCurInfo.isSelectMode()) {
                if (mFragment.getSelectedFileCount() == 0) {
                    updateForBottomDetailLeftFocusedView();
                } else {
                    updateForBottomDetailView();
                }
            } else {
                updateForBottomDetailLeftFocusedView();
            }
        }
    }

    @Override
    public void onItemUpdated() {
        Log.d(this, "onItemUpdated");
        if (isCurInfoValid()) {
            if (MouseKeyboardMgr.isCtrlCopied() || MouseKeyboardMgr.isCtrlMoved()) {
                NavigationInfo info = mFragment.getNavigationInfo();
                if (info == null || info.needShowCopyMoveBottomDetail()) {
                    updateForBottomDetailBTView();
                }
            } else {
                if (mFragment.isSelectActionMode()) {
                    updateForBottomDetailView();
                } else {
                    updateForBottomDetailLeftFocusedView();
                }
            }
        }
    }

    private boolean isCurInfoValid() {
        boolean bRet = false;
        mCurInfo = NavigationManager.getInstance(mProcessId).getCurInfo();
        if (mCurInfo != null) {
            mFragment = mCurInfo.getCurFragment();
            if (mFragment != null) {
                bRet = true;
            }
        }
        return bRet;
    }

    private void updateForBottomDetailView() {
        mRecordList.clear();
        mRecordList.addAll(mFragment.getSelectedFile());
        updateView(BottomDetailView.BottomViewType.BottomDetailView);
    }

    private void updateForBottomDetailLeftFocusedView() {
        mRecordList.clear();
        mRecordList.add(mCurInfo.getCurRecord());
        updateView(BottomDetailView.BottomViewType.BottomDetailLeftFocusedView);
    }

    private void updateForBottomDetailBTView() {
        mRecordList.clear();
        mRecordList.addAll(MouseKeyboardMgr.getSelectedFile());
        updateView(BottomDetailView.BottomViewType.BottomDetailBTView);
    }

    private void clearBottomDetailView() {
        if (mBottomDetailView != null) {
            mBottomDetailView.cancelTask();
            mBottomDetailView.setVisibility(GONE);
        }
    }

    private BottomDetailView getBottomDetailView(BottomDetailView.BottomViewType viewKey) {
        BottomDetailView ret = mBottomViewMap.get(viewKey);
        if (ret == null) {
            ret = BottomDetailView.createView(this, mContext, viewKey);
            mBottomViewMap.put(viewKey, ret);
        }
        return ret;
    }

    private void updateView(BottomDetailView.BottomViewType viewKey) {
        clearBottomDetailView();
        mBottomDetailView = getBottomDetailView(viewKey);
        if (mBottomDetailView != null) {
            mBottomDetailView.setVisibility(VISIBLE);
            mBottomDetailView.updateView(mRecordList, mFragment);
            mBottomDetailView.updateThumbnailView();
        }
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (mBottomDetailView != null) {
            mBottomDetailView.refreshChildView();
        }
    }
}